package bpm.dao.impl.document;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;

import bpm.domain.document.DocumentDefinition;
import bpm.domain.document.Template;
import bpm.domain.document.TemplateType;
import bpm.domain.user.OrgUser;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public class TemplateMapper implements RowMapper {
	private boolean content = false;
	private boolean previewImage = false;
	
	public TemplateMapper(boolean content, boolean previewImage) {
		this.content = content;
		this.previewImage = previewImage;
	}

	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		Template template = new Template();
		
		template.setId(rs.getInt("id"));
		template.setVersion(rs.getInt("version"));
		template.setName(rs.getString("name"));
		template.setType(TemplateType.valueOf(rs.getString("type")));
		
		int documentDefinitionId = rs.getInt("docdef_id");
		if(documentDefinitionId != 0) {
			template.setDocumentDefinition(new DocumentDefinition(documentDefinitionId));
		}
		String creatorId = rs.getString("creator_id");
		if(creatorId != null) {
			template.setCreator(new OrgUser(creatorId));
		}
		
		template.setCreateTime(new Date(rs.getDate("create_time").getTime()));
		template.setLastUpdateTime(new Date(rs.getDate("last_update_time").getTime()));
		template.setDescription(rs.getString("description"));
		
		if(content) {
			template.setContent(rs.getBytes("content"));
		}
		if(previewImage) {
			template.setPreviewImage(rs.getBytes("preview_image"));
		}
		
		return template;
	}

}
